/**
 * Created by no1 on 2015/8/11.
 */
'use strict';
var DaoBase = require('./DaoBase');
var F = require('../../function');
var db = require('./models')
var util = require("util");

function AppConfigDao(data) {
  data = data || {}
  data.model = data.model || db.appConfig;
  this.data = data;
  DaoBase.call(this, this.data);
};
util.inherits(AppConfigDao, DaoBase);

/**
 * @todo 保存
 * @param query
 * @param date
 * @returns {{error, msg, data}|{error, msg}}
 */
AppConfigDao.prototype.findAndSave = function*(query, date) {
  if (!(query || date)) return F.returnData(1, "参数有问题.");
  let ret = yield this.model.findOne(query);
  if (ret != null) {
    if (ret.errors) return F.returnData(1, '查找出错了', ret.errors);
    if (ret._doc._id) return F.returnData(1, '已经有数据.', ret._doc);
  }
  ret = yield this.save(date);
  if (ret.error == 1) return F.returnData(1, ret.msg);
  return F.returnData(0, ret.msg, ret.date);
};

module.exports = AppConfigDao;